package com.itheima.admin.controller.v1;

import com.itheima.admin.service.RoleService;
import com.itheima.model.admin.dtos.RoleDto;
import com.itheima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色管理控制器
 * 
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    @ApiOperation("查询所有角色列表")
    public ResponseResult listAllRoles() {
        return roleService.listAllRoles();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询角色详情")
    public ResponseResult getRoleById(@PathVariable Integer id) {
        return roleService.getRoleById(id);
    }

    @PostMapping
    @ApiOperation("创建角色")
    public ResponseResult createRole(@RequestBody RoleDto dto) {
        return roleService.createRole(dto);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新角色")
    public ResponseResult updateRole(@PathVariable Integer id, @RequestBody RoleDto dto) {
        return roleService.updateRole(id, dto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除角色")
    public ResponseResult deleteRole(@PathVariable Integer id) {
        return roleService.deleteRole(id);
    }

    @PutMapping("/{id}/permissions")
    @ApiOperation("分配权限给角色")
    public ResponseResult assignPermissions(@PathVariable Integer id, @RequestBody Map<String, List<Integer>> params) {
        List<Integer> permissionIds = params.get("permissionIds");
        return roleService.assignPermissions(id, permissionIds);
    }
}

